/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.cn360.batch.BuildCn360BrandJob
   Module Description   :

   Date Created      : 2011/6/21
   Original Author   : jeffma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.cn360.batch;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.csvreader.CsvWriter;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.core.thread.batch.AbstractRunJob;
import com.yaodian100.ec.cn360.remote.RemoteCn360ProductService;
import com.yaodian100.ec.product.entity.Brand;
import com.yaodian100.ec.product.service.ProductService;

/**
 * @author jeffma
 * 
 */
public class BuildCn360BrandJob extends AbstractRunJob {

	private ProductService productService;

	/** default constructor */
	public BuildCn360BrandJob(ProductService productService) {
		super("BuildCn360BrandJob");
		this.productService = productService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.core.thread.batch.AbstractRunJob#execute()
	 */
	@Override
	public void execute() {
		String feedFile = ServerValue.getFrontApachePath() + "3party_ex" + File.separator + "360cn" + File.separator
				+ "yaodian100_b.csv";
		File feedFileObj = new File(feedFile);
		if(feedFileObj.exists()) {
			feedFileObj.delete();
		}
		try {
			CommonCriteria cri = new CommonCriteria();
			List<Brand> brands = productService.getBrandList(0, -1, cri, new String[] { "brandId asc" });

			CsvWriter writer = new CsvWriter(feedFile, ',', Charset.forName("gb2312"));
			writer.writeRecord(new String[] { "bid", "id", "brand_name", "brand_ename", "general_name", "alias_name",
					"short_title", "website_url", "description", "imgs", "createtime" });
			int count = 0;
			String[] data = new String[] { RemoteCn360ProductService.BID, "", "", "", "", "", "", " ", "", "", "" };
			data[0] = RemoteCn360ProductService.BID;
			for (Brand b : brands) {
				Long brandId = Long.valueOf(StringUtils.substring(b.getBrandId(), 2));
				data[1] = brandId.toString(); // local brand id
				data[2] = b.getBrandName(); // brand name
				if (StringUtils.isBlank(data[2])) {
					data[2] = b.getBrandEname();
				}
				data[3] = b.getBrandEname(); // brand ename
				writer.writeRecord(data);
				count++;
				if (count % 200 == 0) {
					writer.flush();
				}
			}
			writer.flush();
			writer.close();
		} catch (Exception e) {
			logger.error("写档错误", e);
		}
	}
}
